package com.unience.ec2.eclipse.config;

import java.util.HashMap;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

public class UtilSWT {
	
	
	static HashMap<String, Image> imageMap = new HashMap<String, Image>();
	
	public static Shell getShell(){
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();	
		Shell shell = new Shell (display);
		centerShell(shell, display);
		
		return shell;
	
	}

	public static Display getDisplay(){
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();	
		return display;
	}
	
	public static void centerShell(Shell shell, Display display){
		Monitor primary = display.getPrimaryMonitor ();
		Rectangle bounds = primary.getBounds ();
		Rectangle rect = shell.getBounds ();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation (x, y);

	}
	
	public static Image getImage(String imageId){
		
		Image image = imageMap.get(imageId);
		
		if (image == null){
			ImageDescriptor descriptor = ImageDescriptor.createFromFile(
					Config.class, imageId);
			image = descriptor.createImage();
			
			imageMap.put(imageId, image);
			
		}
		
		return image;
		
	}
}
